package com.eshop.service.Impl;

import com.eshop.dao.ProductInfoDao;
import com.eshop.pojo.Pager;
import com.eshop.pojo.ProductInfo;
import com.eshop.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ProductInfoService")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class ProductInfoServiceImpl implements ProductInfoService {
    @Autowired
    private ProductInfoDao productInfoDao;

    @Override
    public List<ProductInfo> selectByPage(ProductInfo productInfo, Pager pager) {
        Map<String,Object> param = new HashMap<>();
        param.put("productInfo",productInfo);
        int resultCount = productInfoDao.count(param);
        if (resultCount > 0) {
            pager.setRowCount(resultCount);
            param.put("pager",pager);
        }
        return productInfoDao.selectByPage(param);
    }

    @Override
    public int count(Map<String, Object> param) {
        return productInfoDao.count(param);
    }

    @Override
    public void insertProduct(ProductInfo productInfo) {
        productInfoDao.insertProduct(productInfo);
    }

    @Override
    public void edit(ProductInfo productInfo) {
        productInfoDao.edit(productInfo);
    }

    @Override
    public void updateStatus(String ids, int flag) {
        productInfoDao.updateStatus(ids,flag);
    }

    @Override
    public List<ProductInfo> getOnSaleProducts() {
        return productInfoDao.getOnSaleProducts();
    }

    @Override
    public ProductInfo selectById(int id) {
        return productInfoDao.selectById(id);
    }

    @Override
    public int deleteProduct(String ids) {
        return productInfoDao.deleteProduct(ids);
    }

    @Override
    public void sell(int productId, int nums) {
        productInfoDao.sell(productId, nums);
    }
}
